package org.isouland.cpwx.api.base;

import org.isouland.cpwx.api.common.JsapiToken;
import org.isouland.cpwx.api.core.util.HttpUtil;

import com.alibaba.fastjson.JSONObject;

public class WxJsapiTokenAPI {

	public final static String jsapi_token_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";  
	
	public static JsapiToken getJsapiToken(String accessToken){
		JsapiToken jsapiToken = null;
		String requestUrl = jsapi_token_url.replace("ACCESS_TOKEN", accessToken);
	    JSONObject jsonObject = HttpUtil.sendGet(requestUrl);  
	    if (null != jsonObject) {
	    	try{
	    		jsapiToken = new JsapiToken();
	    		jsapiToken.setTicket(jsonObject.getString("ticket"));
	    		jsapiToken.setExpiresIn(jsonObject.getIntValue("expires_in"));
	    	} catch(Exception e){
	    		jsapiToken = null;
	    	}
	    }
		return jsapiToken;
	}
}
